package com.boodaba.todocarts.test.app;

import static com.googlecode.instinct.expect.Expect.expect;

import java.util.HashMap;
import java.util.Map;

import org.jmock.Expectations;
import org.junit.runner.RunWith;

import com.boodaba.todocarts.app.ITodoSettingsActivity;
import com.boodaba.todocarts.app.TodoSettingsPresenter;
import com.boodaba.todocarts.domain.ILabelRepository;
import com.boodaba.todocarts.domain.Label;
import com.googlecode.instinct.integrate.junit4.InstinctRunner;
import com.googlecode.instinct.marker.annotate.BeforeSpecification;
import com.googlecode.instinct.marker.annotate.Mock;
import com.googlecode.instinct.marker.annotate.Specification;
import com.googlecode.instinct.marker.annotate.Subject;

@RunWith(InstinctRunner.class)
public class TodoSettingsPresenterSpecs2 {
	@Subject private TodoSettingsPresenter SUT;
	
	@Mock ITodoSettingsActivity todoSettingsActivity;
	@Mock ILabelRepository labelRepository;
	
	private Label trgLabel;
	
	private final String trgLabelTitle = "Label target";
	private final String todoDone1Title = "toDo one done";
	private final String todoActive1Title = "Too twO active";
	private final String todoActive2Title = "ttt Three one active";
	
	
	@BeforeSpecification
	public void setUp() {
		SUT = new TodoSettingsPresenter(labelRepository);
		trgLabel = new Label(trgLabelTitle);
		expect.that(new Expectations() {{
			allowing(todoSettingsActivity).getTargetLabelTitle();
				will(returnValue(trgLabelTitle));
			allowing(labelRepository).getLabelByTitle(with(equal(trgLabelTitle)));
				will(returnValue(trgLabel));
		}});
		setInited();
	}
	
	@Specification
	public void shouldChargeItemAndSaveLabelIfSelectCompletedTodo() {
		expect.that(trgLabel.getTodoByTitle(todoDone1Title).isCompleted()).isTrue();
		expect.that(new Expectations() {{
			oneOf(todoSettingsActivity).getSelected(); will(returnValue(todoDone1Title));
			oneOf(todoSettingsActivity).chargeInWorkingList();
			oneOf(labelRepository).save(trgLabel);
		}});
		SUT.checkItem();
		expect.that(trgLabel.getTodoByTitle(todoDone1Title).isCompleted()).isFalse();
	}
	
	@Specification
	public void shouldDischargeItemAndSaveLabelIfSelectActiveTodo() {
		expect.that(trgLabel.getTodoByTitle(todoActive2Title).isCompleted()).isFalse();
		expect.that(new Expectations() {{
			oneOf(todoSettingsActivity).getSelected(); will(returnValue(todoActive2Title));
			oneOf(todoSettingsActivity).dischargeInWorkingList();
			oneOf(labelRepository).save(trgLabel);
		}});
		SUT.checkItem();
		expect.that(trgLabel.getTodoByTitle(todoActive2Title).isCompleted()).isTrue();
	}
	
	void setInited() {
		trgLabel.inviteTodo(todoActive1Title);
		trgLabel.inviteTodo(todoActive2Title);
		trgLabel.inviteTodo(todoDone1Title);
		trgLabel.completeTodo(todoDone1Title);
		expect.that(new Expectations() {{
			Map<String, Boolean> chkMap = new HashMap<String, Boolean>();
			chkMap.put(todoActive1Title, false);
			chkMap.put(todoActive2Title, false);
			chkMap.put(todoDone1Title, true);
			oneOf(todoSettingsActivity).setWorkingTitle(with(equal(trgLabelTitle)));
			oneOf(todoSettingsActivity).setWorkingList(with(equal(chkMap)));
		}});
		SUT.initializeWith(todoSettingsActivity);
	}
}
